package com.lrm.dao;

import com.lrm.NotFoundException;
import com.lrm.po.Tag;
import com.lrm.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TagDao  {
    public static Tag findByName(String name){
        Tag tag=null;
        try{
            Connection con= DBUtils.getConnection();
            String sql="select * from t_tag where name=?";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setObject(1,name);
            ResultSet result=ps.executeQuery();

            if(result.next()){
                tag=new Tag();
                tag.setName(result.getString("name"));
                tag.setId(result.getLong("id"));

            }
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        return tag;
    }

    public static void delete(Long id){
        try{
            Connection con= DBUtils.getConnection();
            String sql="call delete_tag("+id+");";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.execute(sql);
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    public static Tag save(Tag t){
        try{
            Connection con= DBUtils.getConnection();
            String sql="call insert_tag("+t.getId()+","+"'"+t.getName()+"'"+");";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.execute();
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        return t;
    }

    public static Tag findOne(Long id){
        Tag tag=null;
        try{
            Connection con= DBUtils.getConnection();
            String sql="select * from t_tag where id=?";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setObject(1,id);
            ResultSet result=ps.executeQuery();
            if(result.next()){
                tag=new Tag();
                tag.setName(result.getString("name"));
                tag.setId(result.getLong("id"));
            }
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        return tag;
    }

    public static List<Tag> findAll() {
        List<Tag> list=new ArrayList<Tag>();
        try{
            Connection con= DBUtils.getConnection();
            String sql="select * from t_tag";
            PreparedStatement ps=con.prepareStatement(sql);
            ResultSet result=ps.executeQuery();
            while(result.next()){
                Tag tag=new Tag();
                tag.setName(result.getString("name"));
                tag.setId(result.getLong("id"));
                list.add(tag);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
       return list;
    }

    public static Tag updateTag(Long id,String name){
        Tag t = TagDao.findOne(id);
        if (t == null) {
            throw new NotFoundException("不存在该标签");

        }
        try{
            Connection con= DBUtils.getConnection();
            String sql="update t_tag set name=? where id=?";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setObject(1,name);
            ps.setObject(2,id);
            ps.executeUpdate();
            t.setName(name);
        }catch(Exception e){
            e.printStackTrace();
        }
       return t;
    }
}
